﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using PS.DBUtility;
namespace PS.DAL
{
    //SalaryFile
    public partial class SalaryFile
    {

        public bool Exists(int SalaryFileID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SalaryFile");
            strSql.Append(" where ");
            strSql.Append(" SalaryFileID = @SalaryFileID  ");
            SqlParameter[] parameters = {
					new SqlParameter("@SalaryFileID", SqlDbType.Int,4)};
            parameters[0].Value = SalaryFileID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(PS.Model.SalaryFile model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SalaryFile(");
            strSql.Append(" FileName,Description,Comment,DataMonth,DataYear,Spare1,Spare2,SpareDecimal1,SpareDecimal2,Creator,CreateTime,Editor,EditTime,ValidMark");
            strSql.Append(") values (");
            strSql.Append("@FileName,@Description,@Comment,@DataMonth,@DataYear,@Spare1,@Spare2,@SpareDecimal1,@SpareDecimal2,@Creator,@CreateTime,@Editor,@EditTime,@ValidMark");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			                 
                        new SqlParameter("@FileName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@Comment", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@DataMonth", SqlDbType.Int,4) ,            
                        new SqlParameter("@DataYear", SqlDbType.Int,4) ,            
                        new SqlParameter("@Spare1", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Spare2", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SpareDecimal1", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SpareDecimal2", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Creator", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Editor", SqlDbType.Int,4) ,            
                        new SqlParameter("@EditTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ValidMark", SqlDbType.Int,4)             
              
            };


            parameters[0].Value = model.FileName;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.Comment;
            parameters[3].Value = model.DataMonth;
            parameters[4].Value = model.DataYear;
            parameters[5].Value = model.Spare1;
            parameters[6].Value = model.Spare2;
            parameters[7].Value = model.SpareDecimal1;
            parameters[8].Value = model.SpareDecimal2;
            parameters[9].Value = model.Creator;
            parameters[10].Value = model.CreateTime;
            parameters[11].Value = model.Editor;
            parameters[12].Value = model.EditTime;
            parameters[13].Value = model.ValidMark;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PS.Model.SalaryFile model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SalaryFile set ");

            //strSql.Append(" SalaryFileID = @SalaryFileID , ");
            strSql.Append(" FileName = @FileName , ");
            strSql.Append(" Description = @Description , ");
            strSql.Append(" Comment = @Comment , ");
            strSql.Append(" DataMonth = @DataMonth , ");
            strSql.Append(" DataYear = @DataYear , ");
            strSql.Append(" Spare1 = @Spare1 , ");
            strSql.Append(" Spare2 = @Spare2 , ");
            strSql.Append(" SpareDecimal1 = @SpareDecimal1 , ");
            strSql.Append(" SpareDecimal2 = @SpareDecimal2 , ");
            strSql.Append(" Creator = @Creator , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" Editor = @Editor , ");
            strSql.Append(" EditTime = @EditTime , ");
            strSql.Append(" ValidMark = @ValidMark  ");
            strSql.Append(" where SalaryFileID=@SalaryFileID  ");

            SqlParameter[] parameters = {
                        new SqlParameter("@SalaryFileID", SqlDbType.Int,4) ,            
                        new SqlParameter("@FileName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@Comment", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@DataMonth", SqlDbType.Int,4) ,            
                        new SqlParameter("@DataYear", SqlDbType.Int,4) ,            
                        new SqlParameter("@Spare1", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Spare2", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@SpareDecimal1", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SpareDecimal2", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Creator", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Editor", SqlDbType.Int,4) ,            
                        new SqlParameter("@EditTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ValidMark", SqlDbType.Int,4)             
              
            };
            int Pindex = 0;
            parameters[Pindex++].Value = model.SalaryFileID;
            parameters[Pindex++].Value = model.FileName;
            parameters[Pindex++].Value = model.Description;
            parameters[Pindex++].Value = model.Comment;
            parameters[Pindex++].Value = model.DataMonth;
            parameters[Pindex++].Value = model.DataYear;
            parameters[Pindex++].Value = model.Spare1;
            parameters[Pindex++].Value = model.Spare2;
            parameters[Pindex++].Value = model.SpareDecimal1;
            parameters[Pindex++].Value = model.SpareDecimal2;
            parameters[Pindex++].Value = model.Creator;
            parameters[Pindex++].Value = model.CreateTime;
            parameters[Pindex++].Value = model.Editor;
            parameters[Pindex++].Value = model.EditTime;
            parameters[Pindex++].Value = model.ValidMark;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int SalaryFileID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SalaryFile ");
            strSql.Append(" where SalaryFileID=@SalaryFileID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SalaryFileID", SqlDbType.Int,4)};
            parameters[0].Value = SalaryFileID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PS.Model.SalaryFile GetModel(int SalaryFileID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SalaryFileID, FileName, Description, Comment, DataMonth, DataYear, Spare1, Spare2, SpareDecimal1, SpareDecimal2, Creator, CreateTime, Editor, EditTime, ValidMark  ");
            strSql.Append("  from SalaryFile ");
            strSql.Append(" where SalaryFileID=@SalaryFileID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SalaryFileID", SqlDbType.Int,4)};
            parameters[0].Value = SalaryFileID;


            PS.Model.SalaryFile model = new PS.Model.SalaryFile();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["SalaryFileID"].ToString() != "")
                {
                    model.SalaryFileID = int.Parse(ds.Tables[0].Rows[0]["SalaryFileID"].ToString());
                }
                model.FileName = ds.Tables[0].Rows[0]["FileName"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Comment = ds.Tables[0].Rows[0]["Comment"].ToString();
                if (ds.Tables[0].Rows[0]["DataMonth"].ToString() != "")
                {
                    model.DataMonth = int.Parse(ds.Tables[0].Rows[0]["DataMonth"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DataYear"].ToString() != "")
                {
                    model.DataYear = int.Parse(ds.Tables[0].Rows[0]["DataYear"].ToString());
                }
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                if (ds.Tables[0].Rows[0]["SpareDecimal1"].ToString() != "")
                {
                    model.SpareDecimal1 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SpareDecimal2"].ToString() != "")
                {
                    model.SpareDecimal2 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = int.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Editor"].ToString() != "")
                {
                    model.Editor = int.Parse(ds.Tables[0].Rows[0]["Editor"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SalaryFile ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 假删除
        /// </summary>
        public void DeleteByYearAndMonth(int DataYear,int DataMonth)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(string.Format("update  SalaryFile Set ValidMark =0 Where DataYear={0} and DataMonth={1} ", DataYear, DataMonth));
      
              DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM SalaryFile ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }



        public DataSet GetList(string strCondition, string filedOrder, long pagesize, long pageindex, out long totalcount)
        {


            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT   a.SalaryFileID, a.FileName, a.Description, a.Comment, a.DataMonth, a.DataYear, a.Spare1, a.Spare2, ");
            strSql.Append(" a.SpareDecimal1, a.SpareDecimal2, a.Creator, a.CreateTime, a.Editor, a.EditTime, a.ValidMark, su.UserName");
            strSql.Append(" from SalaryFile  a left join dbo.SysUser su on su.UserID =a.Editor ");

            if (strCondition.Trim() != "")
            {
                strSql.Append(" where " + strCondition);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            string totalSql = PageSplitHelper.GetTotalSql(strSql.ToString());
            totalcount = long.Parse(DbHelperSQL.ExecuteScalar(totalSql).ToString());

            string pageSql = PageSplitHelper.GetPageSql(strSql.ToString(), pagesize, pageindex, totalcount);

            return DbHelperSQL.Query(pageSql);

        }
    }
}

